import { Base64 } from 'js-base64'
const baseUrl = 'https://meituan.thexxdd.cn/api/';
import { Rescovid, hpv_res, Resnuataapi, Graphics } from './decl-type'
//获取token
function getToken(): string {
    const user = uni.getStorageSync('user') || '';
    const base64_token = Base64.encode(user.user_Token + ':');//此博主后端要求加冒号
    return 'Basic ' + base64_token;
};

function sendRequest(url: string, method: 'GET' | 'POST', data: string | object | ArrayBuffer) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + url,
            method,
            data,
            header: {
                Authorization: getToken()
            },
            success: (res: any) => {
                if (res.statusCode == 200) {

                    resolve(res)
                } else if (res.statusCode == 401) {
                    uni.showToast({
                        title: '请先登录',
                        icon: 'none',
                        duration: 1000//延长时间
                    })
                    reject(res);
                    uni.navigateTo({
                        url: '/pages/login/index'
                    });
                } else if (res.statusCode == 400) {

                    uni.showToast({
                        title: '开发者某个字段或参数填写不对',
                        icon: 'none',
                        duration: 1000//延长时间
                    })
                    reject(res);
                } else if (res.statusCode == 500) {
                    uni.showToast({
                        title: '服务器发生错误！',
                        icon: 'none',
                        duration: 1000//延长时间
                    })
                    reject(res)
                } else if (res.statusCode == 202) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1000//延长时间
                    })
                    reject(res)
                } else {
                    uni.showToast({
                        title: '服务器发生错误！',
                        icon: 'none',
                        duration: 1000//延长时间
                    })
                    reject(res)
                }
            },
            fail: (res) => {
                uni.showToast({
                    title: '服务器发生错误！',
                    icon: 'none',
                    duration: 1000//延长时间
                })
                reject(res)
            }
        })
    })
}
//所有请求接口
const RequestApi = {
    // 首页数据
    FrontPage: () => sendRequest('frontpage', 'GET', {}),
    // 新冠疫苗预约时间数据
    NewappTime: () => sendRequest('newapptime', 'GET', {}),
    //新冠疫苗预约
    Rescovid: (data: Rescovid) => sendRequest('rescovid', 'POST', data),
    //用户登录
    userLogin: (data: loginData) => sendRequest('wx_login', 'POST', data),
    //新冠疫苗预约订单
    coviduserOrder: () => sendRequest('coviduser_order', 'GET', {}),
    //取消新冠疫苗预约订单
    covidCancel: (data: { _id: string }) => sendRequest('covidcancel?query=', 'GET', data),
    //hpv数据列表
    outHpv: () => sendRequest('otuhpv', 'GET', {}),
    //hpv疫苗套餐
    hpv_pack: () => sendRequest('hpv_pack', 'GET', {}),
    //hpv价格请求
    hpv_price: (data) => sendRequest('hpv_price', 'POST', data),
    //hpv预约
    hpv_res: (data: hpv_res) => sendRequest('reshpv', 'POST', data),
    //hpv预约订单
    hpvuser_order: () => sendRequest('hpvuser_order', 'GET', {}),
    //hpv订单取消
    hpvorderCancel: (data: { _id: string }) => sendRequest('hpvcancel?query=', 'GET', data),
    //得到核酸检测信息
    nuataget: () => sendRequest('nuataget', 'GET', {}),
    //提交核酸检测预约
    ResNuata: (data: Resnuataapi) => sendRequest('resnuata', 'POST', data),
    // 核酸检测订单
    NuatauserOrder: () => sendRequest('nuatauser_order', 'GET', {}),
    // 取消核酸检测预约
    NuataCancel: (data: { _id: string }) => sendRequest('nuatacancel', 'GET', data),
    // 获取就诊人信息
    GetpaTient: () => sendRequest('get_patient', 'GET', {}),
    // 提交就诊人
    PatientRes: (data: Patientres) => sendRequest('patient_res', 'POST', data),
    // 提交图文咨询
    GrapHics: (data: Graphics) => sendRequest('graphics', 'POST', data),
    // 33. 获取父科室列表    GET    /department
    DeparTment: () => sendRequest('department', 'GET', {}),
    // 34. 获取父科室下的子科室   GET   /reglist?query=
    RegList: (data: { id: string }) => sendRequest('reglist', 'GET', data),
    // 35. 获取选择医生的日期    GET   /timesele?query=
    TimeSele: (data: { dep_id: string }) => sendRequest('timesele', 'GET', data),
    // 36. 获取选择的科室下的全部医生列表     GET    /alldlist?query=
    AlldList: (data: { dep_id: string }) => sendRequest('alldlist', 'GET', data),
    // 37. 根据时间筛选挂号医生    GET    /everydlist?query=
    EverydList: (data: { dep_id: string; week: string }) =>
        sendRequest('everydlist', 'GET', data),
    // 38. 挂号医生主页   GET   /doctorhome?query=
    DoctorHome: (data: { _id: string }) => sendRequest('doctorhome', 'GET', data),
    // 39. 提交挂号预约   POST   /regappoin
    RegAppoin: (data: Regappoin) => sendRequest('regappoin', 'POST', data),
    // 40. 获取每个用户的挂号记录    GET    /user_registrat
    UserreGistrat: () => sendRequest('user_registrat', 'GET', {}),
    // 41. 取消预约挂号    GET   /regist_cancel?query=
    RegistCancel: (data: { _id: string }) => sendRequest('regist_cancel', 'GET', data),
    // 获取体检筛选条件
    PhyTerm: () => sendRequest('phyterm', 'GET', {}),
    // 获取全部体检套餐
    PhySget: () => sendRequest('physget', 'GET', {}),
    // 查询筛选体检套餐
    PhyQuery: (data: FilterData) => sendRequest('phyquery', 'POST', data),
    // 体检套餐详情页数据
    PhydaTeil: (data: PhyId) => sendRequest('phydateil', 'GET', data),
    // 体检预约提交
    ResPhy: (data: PhyRes) => sendRequest('resphy', 'POST', data),
    //获取体检套餐订单
    PhyuserOrder: () => sendRequest('phyuser_order', 'GET', {}),
    // 24. 取消体检预约
    PhyCancel: (data: Covidcancelapi) => sendRequest('phycancel', 'GET', data),
    // 26. 获取抑郁症题目    GET    /depression_topics
    DepressionTopics: () => sendRequest('depression_topics', 'GET', {}),
    // 27. 抑郁症测试结果   GET   /depression?query=
    DepressiSon: (data: { value: string[] }) =>
        sendRequest('depression', 'GET', data),
    // 28. 获取早泄题目    GET   /premature_topics
    PrematureTopics: () => sendRequest('premature_topics', 'GET', {}),
    // 29. 早泄测试结果    GET   /premature?query=
    PremaTure: (data: { value: string[] }) => sendRequest('premature', 'GET', data),
    // 30. 获取失眠题目    GET   /insomnia_topics
    InsomniaTopics: () => sendRequest('insomnia_topics', 'GET', {}),
    // 31. 失眠测试结果    GET   /insomnia?query=
    InsoMnia: (data: { value: string[] }) => sendRequest('insomnia', 'GET', data),
    // 32. 获取短视频数据    GET   /video_list?query=
    VideoList: (data: { page: number }) => sendRequest('video_list', 'GET', data),
    //文件上传
    uploadImg: baseUrl + 'upload_picture',
    //身份认证
    indentfy: baseUrl + 'ai_card'
}
export default RequestApi;





